/*
 * JDialog.java
 *
 * Created on October 8, 2004, 12:28 PM
 */

package edu.umd.essic.maskbuilder;

/**
 *
 * @author  Administrator
 */
public class ESSICDialog extends javax.swing.JDialog {
    /** A return status code - returned if Cancel button has been pressed */
    private static final int RET_CANCEL = 0;
    /** A return status code - returned if OK button has been pressed */
    private static final int RET_OK = 1;
    public boolean OnOK;
    public boolean flag;
    public int NMAX;
    Object[][] data =  new Object [][] {
                {new Float(310), new Float(59), new Float(170),new Float(500), new Float(40), new Float(0)},
                {new Float(0), new Float(5), new Float(165),new Float(0), new Float(5), new Float(-20)},
                {new Float(0), new Float(1), new Float(1),new Float(0), new Float(1), new Float(0)},
                {new Float (0), new Float(1), new Float(1),new Float (0), new Float(1), new Float(0)},
                {new Float(0), new Float(1), new Float(1),new Float(0), new Float(1), new Float(0)}
            };
     Object[] header = new String [] {
                "alpha_x", "beta_x", "gamma_x","alpha_y", "beta_y", "gamma_y"};
    /** Creates new form JDialog */
    public ESSICDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    public float[][] getReturnStatus() {
        if (returnStatus == RET_OK){
            NMAX = 0;
            float[][] areas = new float[5][6];
            for(int i=0;i<5;i++){
                for(int j=0;j<6;j++){
                    areas[i][j] = Float.parseFloat(jTable2.getValueAt(i,j).toString());
                }
                if ((areas[i][0] != 0.0)||(areas[i][3] != 0.0))
                    NMAX++;
            }
            for (int i = 0; i<5; i++){
                if (areas[i][2] < 0) // if western hemisphere we'll recalculate degree value for Seapodym
                    areas[i][2] = 360 + areas[i][2];
            }
            return areas;
        }else 
            return null;
    }
    
    private boolean CheckValues(){
        boolean answer = true;
        float[][] values = new float[5][6];
        for(int i=0;i<5;i++){
            for(int j=0;j<6;j++){
                try {
                    values[i][j] = Float.parseFloat(jTable2.getValueAt(i,j).toString());
                } catch (NumberFormatException e){ return false;}
            }
            if ((values[i][1]==0)||(values[i][4]==0)) // betas = 0 -> division by 0 error
                answer = false;
            if ((Math.abs(values[i][2]) > 360)||(Math.abs(values[i][5]) > 360)) // 
                answer = false;
        }
        return answer;
    }
    
   /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        buttonPanel = new javax.swing.JPanel();
        EquatorCheckBox = new javax.swing.JCheckBox();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setTitle("Enter coefficients for ESSIC function");
        setName("EssicDialog");
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setMaximumSize(new java.awt.Dimension(600, 300));
        jScrollPane1.setMinimumSize(new java.awt.Dimension(500, 100));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(810, 100));
        jScrollPane1.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        jTable2.setBorder(new javax.swing.border.CompoundBorder());
        jTable2.setModel(new javax.swing.table.DefaultTableModel(data, header));
        jTable2.setAutoCreateColumnsFromModel(false);
        jTable2.setMaximumSize(new java.awt.Dimension(1024, 768));
        jTable2.setMinimumSize(new java.awt.Dimension(200, 100));
        jTable2.setPreferredSize(new java.awt.Dimension(200, 100));
        jTable2.setRequestFocusEnabled(false);
        jScrollPane1.setViewportView(jTable2);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        buttonPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        buttonPanel.setPreferredSize(new java.awt.Dimension(810, 39));
        EquatorCheckBox.setSelected(true);
        EquatorCheckBox.setText("Don't move equator!");
        EquatorCheckBox.setMaximumSize(new java.awt.Dimension(350, 27));
        EquatorCheckBox.setPreferredSize(new java.awt.Dimension(320, 27));
        buttonPanel.add(EquatorCheckBox);

        okButton.setText("OK");
        okButton.setMaximumSize(new java.awt.Dimension(75, 29));
        okButton.setPreferredSize(new java.awt.Dimension(75, 27));
        okButton.setSelected(true);
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        buttonPanel.add(okButton);

        cancelButton.setText("Cancel");
        cancelButton.setPreferredSize(new java.awt.Dimension(75, 29));
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        buttonPanel.add(cancelButton);

        getContentPane().add(buttonPanel, java.awt.BorderLayout.SOUTH);

        jLabel1.setBackground(new java.awt.Color(255, 255, 255));
        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 14));
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/umd/essic/maskbuilder/Eformula.jpg")));
        jLabel1.setBorder(new javax.swing.border.TitledBorder(null, "Inverse function", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("MS Sans Serif", 0, 13), new java.awt.Color(50, 50, 50)));
        jLabel1.setPreferredSize(new java.awt.Dimension(810, 109));
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        getContentPane().add(jLabel1, java.awt.BorderLayout.NORTH);

        pack();
    }//GEN-END:initComponents

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        getRootPane().setDefaultButton(okButton);        
    }//GEN-LAST:event_formComponentShown

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
        doClose(RET_CANCEL);
        OnOK = false;
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        if (!CheckValues()){
            javax.swing.JOptionPane.showMessageDialog(this,"Wrong value!","Error",javax.swing.JOptionPane.ERROR_MESSAGE);
        } else {
            doClose(RET_OK);
            OnOK = true;
            flag = EquatorCheckBox.isSelected();
        }
    }//GEN-LAST:event_okButtonActionPerformed
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {
        doClose(RET_CANCEL);
    }
    
    private void doClose(int retStatus) {
        returnStatus = retStatus;
        setVisible(false);
        dispose();
    }
    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
         new ESSICDialog(new javax.swing.JFrame(), true).show();
    }*/
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox EquatorCheckBox;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable2;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables
    private int returnStatus = RET_CANCEL;
}
